package Graffiti

import (
	"regexp"
	"strconv"
)

//██文██本██涂██鸦██
func Text(s string, level int) string {
	compile := regexp.MustCompile(`\[CT\d{1,5}].*?\[/CT]`) //匹配的level数字长度为1-5位数，不在这个范围会匹配失败
	return compile.ReplaceAllStringFunc(s, func(ss string) string {
		l, str := getLevelAndString(ss)

		if level >= l {
			return str
		} else {
			var strC string
			for _, s2 := range str {
				if len(string(s2)) > 1 {
					strC += "██"
				} else {
					strC += "█"
				}
			}
			return strC
		}
	})
}

func getLevelAndString(s string) (level int, str string) {
	//[CT8]sssss[/CT]
	//获取level
	levelCompile := regexp.MustCompile(`^\[CT|].*\[/CT]`)
	levelStr := levelCompile.ReplaceAllString(s, "")
	level, _ = strconv.Atoi(levelStr)

	//获取string
	ss := regexp.MustCompile(`^\[CT\d{1,5}]|\[/CT]$`) //匹配的level数字长度为1-5位数，不在这个范围会匹配失败
	str = ss.ReplaceAllString(s, "")
	return
}
